package com.coco.area.service.impl;

import com.coco.area.mapper.AreaMapper;
import com.coco.area.pojo.TbArea;
import com.coco.area.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.coco.area.service.impl
 * @Author: 张dy
 * @Date: Created in 2021/7/19 下午 3:20
 */

@Service
public class AreaServiceImpl implements AreaService {


    @Autowired(required = false)
    private AreaMapper areaMapper;

    @Override
    public List<TbArea> findAll() {
        return areaMapper.findAll();
    }

    @Override
    public List<TbArea> selectByPid(Integer id) {
        return areaMapper.selectByPid(id);
    }


    @Override
    public List<Map> findAllTree(Integer id) {
        List<TbArea> all = areaMapper.selectByPid(id);
        List<Map> maps = new ArrayList<>();
        all.forEach(i -> {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("id", i.getId());
            map.put("name", i.getName());
            map.put("parent_id", i.getParentId());
            map.put("data", findAllTree(i.getId()));
            maps.add(map);
        });
        return maps;
    }


}
